define(function(require,exports,module){
    var Form = require("form");
    var store = require("store");

    module.exports = {
        initComponent: function(widget){
            var form = new Form({
                parentNode: ".J-subForm",
                model: {
                    id: "subForm",
                    saveaction: function(){
                    	widget.handler.subForm.handleSaveBtnClick(widget);
                    },
                    cancelaction: function(){
                    	widget.toCard();
                    },
                    layout: "2_1",
                    items: [{
                    	name: "pkProductRuleDetail",
                    	type: "hidden"
                    },{
                    	name: "version",
                    	type: "hidden",
                    	defaultValue: 0
                    },{
                    	name: "rowIndex",
                    	type: "hidden",
                    	disabled: true
                    },{
                        name: "productType",
                        label: "产品类型",
                        type: "select",
                        url: "api/producttype/query",
                        params: function(){
                        	return {
                        		product: widget.get("form").getValue("product"),
                        		seal: false
                        		
                        	}
                        },
                        keyField: "pkProductType",
                        valueField: "name",
                        validate: ["required"],
                        lazy: true
                    },{
                        name: "productBatch",
                        label: "产品批次",
                        type: "select",
                        url: "api/productbatch/query",
                        params: function(){
                        	return {
                        		product: widget.get("form").getValue("product"),
                        		seal: false
                        	}
                        },
                        keyField: "pkProductBatch",
                        valueField: "name",
                        lazy: true
                    },{
                        name: "price",
                        label: "价格（元）",
                        validate: ["required"]
                    },{
                        name: "validity",
                        label: "有效期限（年）",
                        validate: ["required"],
                        exValidate: function(value){
                        	if(/^[0-9]*[1-9][0-9]*$/.test(value)){
                        		return true;
                        	}else{
                        		return "请输入一个正确的正整数";
                        	}
                        }
                    },{
                        name: "liveDays",
                        label: "居住期限",
                        events: {
                        	"blur": function(e){
                        		var el = $(e.currentTarget);
                        		var val = el.val();
                        		if(val != undefined && val != ""){
                        			if($("div[el-data-component=unit]").find(".required").length == 0){
                        				form.addRules("unit",{
                            				required: true
                            			});
                        			}
                        			form.setReadonly("unit",false);
                        		}else{
                        			form.removeRules("unit",{
                        				required: true
                        			});
                        			form.setReadonly("unit",true);
                        		}
                        	}
                        },
                        exValidate: function(value){
                        	if(/^[0-9]*[1-9][0-9]*$/.test(value) || value == ""){
                        		return true;
                        	}else{
                        		return "请输入一个正确的正整数";
                        	}
                        }
                    },{
                        name: "unit",
                        label: "期限单位",
                        type:"select",
                        options: [{
                        	key: "Month",
                        	value: "月"
                        },{
                        	key: "Day",
                        	value: "天/间"
                        }],
                        readonly: true
                    },{
                        name: "settlementsRules_Expense",
                        label: "费用抵扣规则",
                        type: "select",
                        keyField: "pkSettlementsRule",
                        valueField: "name",
                        lazy:true
                    },{
                        name: "ruleScope_Expense",
                        label: "费用抵扣规则适用范围",
                        type: "select",
                        keyField: "pkScope",
                        valueField: "name",
                        lazy:true,
                        multi: true
                    },{
                        name: "settlementsRules_BedSubsidy",
                        label: "床位费规则",
                        type: "select",
                        keyField: "pkSettlementsRule",
                        valueField: "name",
                        lazy:true
                    },{
                        name: "ruleScope_BedSubsidy",
                        label: "床位费规则适用范围",
                        type: "select",
                        keyField: "pkScope",
                        valueField: "name",
                        lazy:true,
                        multi: true
                    },{
                    	name: "settlementsRules_Coupon",
                    	label: "福利券发放规则",
                    	type: "select",
                        keyField: "pkSettlementsRule",
                        valueField: "name",
                        lazy:true
                    },{
                    	name: "ruleScope_Coupon",
                    	label: "福利券发放规则适用范围",
                    	type: "select",
                        keyField: "pkScope",
                        valueField: "name",
                        lazy:true,
                        multi: true
                    },{
                    	name: "seal",
                    	label: "状态",
                    	type: "select",
                    	options: [{
                    		key:"false",
                    		value: "在售"
                    	},{
                    		key:"true",
                    		value: "停售"
                    	}],
                    	validate: ["required"],
                    	defaultValue: "true"
                    },{
                        name: "description",
                        label: "备注",
                        "break": true,
                        type: "textarea"
                    }]
                }
            });
            widget.set("subForm",form)
        }
    };
});